///////////////////////////////////////////////////////////////////////////////
//
// Copyright
//
///////////////////////////////////////////////////////////////////////////////

package ru.goodcore.composite
{

import flash.events.Event;
import flash.events.EventPhase;
import flash.utils.getClassName;

/**
 * 
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 * 
 */

public class CompositeEvent extends Event {

	//-------------------------------------------------------------------------
	//
	//  Public class constants
	//
	//-------------------------------------------------------------------------

	public static const ADDED:					String = Event.ADDED;

	public static const REMOVED:				String = Event.REMOVED;

	public static const ADDED_TO_CRADLE:		String = 'addedToCradle';

	public static const REMOVED_FROM_CRADLE:	String = 'removedFromCradle';

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function CompositeEvent(eventType:String, bubbles:Boolean=true, cancellable:Boolean=false) {
		super(eventType, bubbles, cancellable);
	}

	//-------------------------------------------------------------------------
	//
	//  Overridden properties: Event
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	internal var $target:	Object = null;

	/**
	 * @inheritDoc
	 */
	public override function get target():Object {
		if ( super.bubbles )
			return this.$target;

		return super.target;
	}

	//----------------------------------
	//  eventPhase
	//----------------------------------

	/**
	 * @inheritDoc
	 */
	public override function get eventPhase():uint {
		if (super.currentTarget !== this.target) {
			//return EventPhase.BUBBLING_PHASE;
			return 3;
		} else {
			//return EventPhase.AT_TARGET;
			return 2;
		}
	}

	//-------------------------------------------------------------------------
	//
	//  Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  Stop Propagation
	//---------------------------------
	internal var $notStopped:	Boolean = true;

	public override function stopImmediatePropagation():void {
		super.stopImmediatePropagation();
		this.$notStopped = false;
	}

	public override function stopPropagation():void {
		super.stopPropagation();
		this.$notStopped = false;
	}

	//---------------------------------
	//  Prevent default
	//---------------------------------
	internal var $defaultPrevented:	Boolean = false;

	public override function isDefaultPrevented():Boolean {
		return this.$defaultPrevented;
	}

	public override function preventDefault():void {
		if (super.cancelable) {
			super.preventDefault();
			this.$defaultPrevented = true;
		}
	}

	//-------------------------------------------------------------------------
	//
	//  Overridden methods: Event
	//
	//-------------------------------------------------------------------------

	/**
	 * @inheritDoc
	 */
	public override function clone():Event {
		return new CompositeEvent(super.type, super.bubbles, super.cancelable);
	}

	/**
	 * @inheritDoc
	 */
	public override function toString():String {
		return this.formatToString(null, 'type', 'bubbles', 'cancelable', 'eventPhase', 'target', 'currentTarget');
	}

	/**
	 * @inheritDoc
	 */
	public override function formatToString(className:String, ...arguments):String {
		if ( !className ) className = getClassName(this);
		( arguments as Array ).unshift( className );
		return super.formatToString.apply( this, arguments );
	}

}
}